package com.example.esp32;

import static android.content.ContentValues.TAG;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class list extends AppCompatActivity {
    /*设备列表*/
    private ListView listView;
    private TextView textView;

    private ListView ZT;
    private List<getValue> ddList = new ArrayList<getValue>();
private Dialog dialog;
    private Button list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_list);
        ProgressDialog progressDialog1 = new ProgressDialog(this);
        progressDialog1.setMessage("正在加载中");
        progressDialog1.show();

        SwipeRefreshLayout srl_my_refresh = findViewById(R.id.lis);
        final Timer t = new Timer();
        t.schedule(new TimerTask() {
            public void run() {
                progressDialog1.dismiss();
                list();
                //结束本界面并跳转到收派员列表的界面
            }
        }, 2000);



        //第二步，设置 下拉刷新时的颜色
        srl_my_refresh.setColorSchemeColors(Color.parseColor("#ff0000"),Color.parseColor("#00ff00"));
        //监听 刷新是回调
        srl_my_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                list();
                srl_my_refresh.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        srl_my_refresh.setRefreshing(false);
                    }
                },30);
            }
        });
        listView = (ListView) findViewById(R.id.testlistview);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {//监听list里面的itme的点击事件
                tck(view);

                Toast.makeText(list.this,"第"+position+"个item", Toast.LENGTH_SHORT).show();


            }
        });

    }

//        list=(Button)findViewById(R.id.button);
//
//        list.setOnClickListener(new View.OnClickListener() {//监听刷新按钮的点击事件
//            @Override
//            public void onClick(View v) {
//
//                list();
//            }
////        });





    public boolean list(){
/*get请求获取数据并更新到lis里*/
             new Thread(){
                @Override
                public void run() {
                    super.run();
                    request request =new request();
//                    String data="wifi"+"="+SSID+"&"+"password"+"="+PAWWER;

                    String resq=request.get("http://ipzhangt:8999/zt");
                    Log.d(TAG, resq);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            listView = (ListView) findViewById(R.id.testlistview);

                            ddList.clear();//自定义适配器的自定义清除防止出现数据溢出


                            try {
                                JSONObject jsonObject = new JSONObject(resq);
                                int itme =jsonObject.optInt("itme");
                                if(itme==0){
                                    Toast.makeText(list.this,"没有设备",Toast.LENGTH_LONG).show();


                                }
                                for(int i=0;i<itme;i++){

                                    getValue dd = new getValue();
                                    String name = jsonObject.optString("sbmc"+i);
                                String zt = jsonObject.optString("sbzt"+i);
                                String ip = jsonObject.optString("ip"+i);
                                dd.setsbmc(zt);
                                dd.setdyzt(name);
                                dd.setip(ip);
                                    ddList.add(dd);
                                    Log.d(TAG,name);
                                }

                                // 封装Java对象

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            ztlistAdapter adapter=new ztlistAdapter(list.this,ddList);

                            listView.setAdapter(adapter);

//                            adapter.setOnItemUpdateClickListener(new ListCardAdapter.onItemUpdateListener() {
//                                @Override
//                                public void onUpdateClick(int i) {
//                                    Toast.makeText(list.this,"还没写这个功能",Toast.LENGTH_LONG).show();
//                                }
//                            });


                        }
                    });
                }
            }.start();

        return true;
    }
    public void tck(View view) {
//     点击后的弹窗暂时用


//        textView = view.findViewById(R.id.mc);
        String str = textView.getText().toString();
        final String[] itme={"开","关","sb"};
        AlertDialog.Builder ale=new AlertDialog.Builder(this);
        ale.setTitle("请选以下功能");
        ale.setItems(itme, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(itme[which]== "开"){
                    publi(str,"kai");
//                    Log.d(TAG, view.findViewById(R.id.zt);
                    Toast.makeText(list.this,"开"+str, Toast.LENGTH_SHORT).show();
                } else if (itme[which]== "关") {
                    publi(str,"guan");
                    Toast.makeText(list.this,"关"+view, Toast.LENGTH_SHORT).show();

                }

            }
        });
ale.show();
    }
    private String publi(String ZHUT,String zl){
        new Thread(){
            @Override
            public void run() {
                super.run();
                JSONObject jsonObject = new JSONObject();

                try {
                    jsonObject.put("zhuti", ZHUT);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }

                try {
                    jsonObject.put("zl", zl);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }

                String a;
                try {
                    a = jsonObject.toString(1);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                request request = new request();
                String data = a;
                String resq = request.post("http://ipzhangt:8999/dn", data);


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                            Toast.makeText(list.this, resq, Toast.LENGTH_SHORT).show();





                    }
                });
            }
        }.start();
        return ZHUT;
    }
}
